package com.pangolin.pangolinim.ui.activity;


import android.text.TextUtils;
import android.widget.Button;
import android.widget.EditText;

import com.orhanobut.logger.Logger;
import com.pangolin.pangolinim.R;
import com.pangolin.pangolinim.app.BroadcastMsg;
import com.pangolin.pangolinim.net.NetFormat;
import com.pangolin.pangolinim.ui.adapter.UIContact;
import com.pangolin.pangolinim.ui.base.BaseActivity;
import com.pangolin.pangolinim.utils.BundleUtil;
import com.pangolin.pangolinim.utils.GsonUtil;
import com.pangolin.pangolinim.utils.UIUtil;

import java.util.HashMap;
import java.util.Map;

public class ChangePassActivity extends BaseActivity {
    private UIContact me;
    private EditText mEtNewPwd;
    private EditText mEtNewPwdConfirm;
    private Button mBtnRePass;

    @Override
    protected void init(){
        String strMe = getIntent().getStringExtra("param1");
        if(strMe != null){
            me = GsonUtil.fromJson(strMe , UIContact.class);
        }
    }

    @Override
    protected void initView(){
        mEtNewPwd = findViewById(R.id.etNewPwd);
        mEtNewPwdConfirm = findViewById(R.id.etNewPwdConfirm);
        mBtnRePass = findViewById(R.id.btnRePass);

        setToolbarTitle(UIUtil.getString(R.string.repass));
    }

    @Override
    protected void initListener(){
        mBtnRePass.setOnClickListener(v -> {
            String newPass= mEtNewPwd.getText().toString();
            String newPassConfirm = mEtNewPwdConfirm.getText().toString();

            if (TextUtils.isEmpty(newPass) || TextUtils.isEmpty(newPassConfirm)){
                UIUtil.showToast("请输入新的密码");
                return;
            }
            if(!newPass.equals(newPassConfirm)){
                UIUtil.showToast("两次输入的密码不相同，请重新输入");
                return;
            }

            Map<String, Object> brocastMap = new HashMap<String, Object>();
            brocastMap.put("type" , BroadcastMsg.DATA_UI_REPASS);
            brocastMap.put("newpasswd" , newPass);
            BundleUtil bundleUtil = new BundleUtil(BroadcastMsg.SIG_UI_DATA , brocastMap , null);
            BroadcastSender(bundleUtil.getBundle());

        });
    }

    @Override
    protected void CoreMessage(BundleUtil bundleUtil){
        if(bundleUtil.getType() == BroadcastMsg.SIG_CORE_DATA){
            if(bundleUtil.getData() != null){
                Map<String, Object> resMap = bundleUtil.getData();
                if((int) resMap.get("type") == BroadcastMsg.DATA_CORE_REPASS){
                    if((int)resMap.get("result") == 0){
                        UIUtil.showToast("修改成功");
                    }else{
                        UIUtil.showToast(NetFormat.ResMsg((int) resMap.get("result")));
                    }
                }
            }
        }
    }

    @Override
    protected int provideContentViewId(){
        return R.layout.activity_change_pass;
    }
}